package cst.a810lw.zyfypt_810lw.Fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import cst.a810lw.zyfypt_810lw.PagerSlidingTabStrip;
import cst.a810lw.zyfypt_810lw.R;


/**
 * A simple {@link Fragment} subclass.
 */
public class PSVideoFragment extends Fragment {
    private VideoFragment videoFragment;
    private SVideoFragment sVideoFragment;
    /**
     * PagerSlidingTabStrip的实例
     */

    private PagerSlidingTabStrip tabs;
    /**
     * 获取当前屏幕的密度
     */

    private DisplayMetrics dm;

    public PSVideoFragment() {
        // Required empty public constructor


    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_psvideo, container, false);
        initView(view);
        return view;
    }

    private void initView(View view) {

        dm = getResources().getDisplayMetrics();
        ViewPager pager = (ViewPager) view.findViewById(R.id.pager);
        tabs = (PagerSlidingTabStrip) view.findViewById(R.id.tabs);
        pager.setAdapter(new MyPagerAdapter(getChildFragmentManager()));
        tabs.setViewPager(pager);
        setTabsValue();

    }

    /**
     * 对PagerSlidingTabStrip的各项属性进行赋值。
     */

    private void setTabsValue() {
        // ����Tab���Զ��������Ļ��
        tabs.setShouldExpand(true);
        // ����Tab�ķָ�����͸����
        tabs.setDividerColor(Color.TRANSPARENT);
        // tabs.setDividerColor(Color.BLACK);
        // ����Tab�ײ��ߵĸ߶�
        tabs.setUnderlineHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 1, dm));
        // ����Tab Indicator�ĸ߶�
        tabs.setIndicatorHeight((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP, 4, dm));// 4
        // ����Tab�������ֵĴ�С
        tabs.setTextSize((int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_SP, 16, dm)); // 16
        // ����Tab Indicator����ɫ
        tabs.setIndicatorColor(Color.parseColor("#45c01a"));// #45c01a
        // ����ѡ��Tab���ֵ���ɫ (�����Զ����һ������)
        tabs.setSelectedTextColor(Color.parseColor("#45c01a"));// #45c01a
        // ȡ�����Tabʱ�ı���ɫ
        tabs.setTabBackground(0);
    }

    // FragmentPagerAdapter FragmentStatePagerAdapter //������FragmentPagerAdapter


    public class MyPagerAdapter extends FragmentStatePagerAdapter {

        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
            // TODO Auto-generated constructor stub
        }

        private final String[] titles = { "最新", "专题" };

        @Override
        public CharSequence getPageTitle(int position) {
            return titles[position];
        }

        @Override
        public int getCount() {
            return titles.length;
        }

        @Override
        public Fragment getItem(int position) {
            switch (position) {
                case 0:

                    if (null == videoFragment) {
                        videoFragment = new VideoFragment();
                    }

                    return videoFragment;

                case 1:

                    if (null == sVideoFragment) {
                        sVideoFragment = new SVideoFragment();
                    }

                    return sVideoFragment;

                default:
                    return null;
            }
        }

    }



}
